import {
	HEADER,
	HEADER2,
	HTTP_REQUEST_URL
} from '../config'

export default function request(api, method, data = {}, {
	notoken = false,
	novali = false,
	jsonType = 1
}) {
	var header = jsonType == 2 ? JSON.parse(JSON.stringify(HEADER2)) : JSON.parse(JSON.stringify(HEADER))
	if (getApp().globalData.token) header['Authorization'] = 'Bearer ' + getApp().globalData.token;
	return new Promise((resolve, reject) => {
		wx.request({
			url: HTTP_REQUEST_URL + api,
			method: method || "get",
			data: data,
			header: header,
			data: data || {},
			success: (res) => {
				if (novali) {
					resolve(res.data)
				} else if (res.data.code === 200) {
					resolve(res.data)
				} else if (res.data.code === 201) {
					resolve(res.data)
				} else if (res.data.code === 500) {
					resolve(res.data)
				} else if (res.data.code === '10001') {
					wx.showToast({
						title: res.data.msg,
						icon: "none"
					})
					reject('请求失败');
				} else if (res.data.code === '10005') {} else {
					// wx.showToast({
					// 	title: res.data.msg,
					// 	icon: 'none'
					// })
					resolve(res.data);
				}

			},
			fail: (msg) => {
				reject('请求失败');
			}
		})
	})
}

['options', 'get', 'post', 'put', 'head', 'delete', 'trace', 'connect'].forEach((method) => {
	request[method] = (api, data, opt) => request(api, method, data, opt || {})
});